package com.exadel.Intlsit;

import java.awt.SystemColor;

public class IntList implements IntegerList {

	private int[] mainData;
	private int[] buffer;
	private int position = 0;
	private int Size = 1;
	
	public IntList(){
		mainData = new int[Size];
		buffer = new int[Size];
	}
	public int get(int i) {
		if(i >= 0 & i < position)
			return mainData[i];
		else
			throw new IndexOutOfBoundsException();
	}
	public void add(int value) {
			if(position < mainData.length){
				mainData[position] = value;
				position++;
			}
			else{
				addMemory();
				mainData[position] = value;
				position++;
			}
	}
	public int size() {
		return position;
	}
	public void insert(int index, int value) {
		if(position < mainData.length){
			insertWhichSystemcopy(index,value);
		}
		else{
			addMemory();
			insertWhichSystemcopy(index,value);
		}
	}
	public void remove(int index) {
		
		if(index >= 0 & index < position){
			System.arraycopy(mainData, index+1, mainData, index, position-index);
			position--;
		}
			
		else
			throw new IndexOutOfBoundsException();
	}
	
	private void addMemory(){
		System.arraycopy(mainData, 0, buffer, 0, mainData.length);
		Size*=3;
		mainData = new int[Size];
		System.arraycopy(buffer, 0, mainData, 0, buffer.length);
		buffer = new int[Size];
	}
	private void insertWhichSystemcopy(int index, int value){
		System.arraycopy(mainData, index, mainData, index+1, position-index);
		mainData[index] = value;
		position++;
	}
	
}
